﻿using Microsoft.AspNetCore.Builder;
using My.Core.UnifyResult;
using My.Core.Util;
using My.Framework;
using My.Framework.AspNetCore;
using Panda.DynamicWebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static NewLife.Remoting.ApiHttpClient;

namespace My.Core.DynamicWebApi
{
	public class DynamicWebApiStartup : StartupModule<DynamicWebApiStartup>
	{
		/// <summary>
		/// 程序启动器
		/// </summary>
		public DynamicWebApiStartup() => Order = 1;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="webApplicationBuilder"></param>
		public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
		{
			var services = webApplicationBuilder.Services;

			services.AddDynamicWebApiCore<ServiceLocalSelectController, ServiceActionRouteFactory>();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="webApplication"></param>
		public override void Configure(WebApplication webApplication)
		{
			webApplication.UseDynamicWebApi((serviceProvider, options) =>
			{
				// 指定全局默认的 api 前缀
				options.DefaultApiPrefix = "apis";

				/**
				 * 清空API结尾，不删除API结尾;
				 * 若不清空 CreatUserAsync 将变为 CreateUser
				 */
				options.RemoveActionPostfixes.Clear();

				/**
				 * 自定义 ActionName 处理函数;
				 */
				options.GetRestFulActionName = (actionName) => actionName;

				
				var assemblyList = AssemblyUtil.GetAssemblyList().ToList();
				foreach (var assembly in assemblyList)
				{
					options.AddAssemblyOptions(assembly);
				}
				//options.AddAssemblyOptions(typeof(OtherService).Assembly);
			});
		}
	}
}
